package com.liveinstars.im.common.dto;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.liveinstars.api.dto.ImGetUserInfo;
import com.liveinstars.api.im.vo.CandyBoxVO;
import com.liveinstars.im.domain.dao.model.CandyBox;
import com.liveinstars.im.domain.dao.model.CandyKidsRecord;
import com.liveinstars.im.entity.ChatMessage;
import com.onepiece.shipelves.common.constant.Constant;
import lombok.Data;
import lombok.ToString;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @author: zhouze
 * @date: 2020/3/24
 */
@Data
@ToString
public class CandyBoxDTO {

    private final static Integer AVERAGE_BOX = 1;

    private final static Integer LUCKY_BOX = 2;

    private final static Integer PERSON_BOX = 3;

    public final static Integer GOLD_CANDY = 1;

    private CandyBox candyBox;

    private CandyBoxVO candyBoxVO;

    public CandyBoxDTO(CandyBox candyBox) {
        this.candyBox = candyBox;
        CandyBoxVO candyBoxVO = new CandyBoxVO();
        candyBoxVO.setId(candyBox.getId());
        candyBoxVO.setStatus(candyBox.getStatus());
        candyBoxVO.setUserNum(candyBox.getUserNum());
        candyBoxVO.setRemark(candyBox.getRemark());
        candyBoxVO.setCandyType(candyBox.getCandyType());
        candyBoxVO.setBoxType(candyBox.getBoxType());
        candyBoxVO.setCandyNum(candyBox.getCandyNum());
        this.candyBoxVO = candyBoxVO;
    }

    public CandyBoxDTO(CandyBoxVO candyBoxVO) {
        this.candyBoxVO = candyBoxVO;
        CandyBox candyBox = new CandyBox();
        candyBox.setId(candyBoxVO.getId());
        candyBox.setStatus(candyBoxVO.getStatus());
        candyBox.setUserNum(candyBoxVO.getUserNum());
        candyBox.setRemark(candyBoxVO.getRemark());
        candyBox.setCandyType(candyBoxVO.getCandyType());
        candyBox.setBoxType(candyBoxVO.getBoxType());
        candyBox.setCandyNum(candyBoxVO.getCandyNum());
        this.candyBox = candyBox;
    }

    public List<CandyKidsRecord> initKidsList(){
        List<CandyKidsRecord> candyKidsList = Lists.newArrayList();
        switch (candyBox.getBoxType()) {
            case 1:
                average(candyKidsList);
                break;
            case 2:
                lucky(candyKidsList);
                break;
            case 3:
                person(candyKidsList);
                break;
            default:
                break;
        }
        return candyKidsList;
    }

    private void average(List<CandyKidsRecord> list){
        long candyNum = candyBox.getCandyNum() / candyBox.getUserNum();
        for (int i = 0; i<candyBox.getUserNum(); i++) {
            CandyKidsRecord candyKidsRecord = new CandyKidsRecord();
            candyKidsRecord.setBoxId(candyBox.getId());
            candyKidsRecord.setCandyNum(candyNum);
            candyKidsRecord.setCandyType(candyBox.getCandyType());
            candyKidsRecord.setNo(i+1);
            candyKidsRecord.setStatus(Constant.NO);
            candyKidsRecord.setState(Constant.STATE_VALID);
            candyKidsRecord.setCstCreated(LocalDateTime.now());
            candyKidsRecord.setCstModified(LocalDateTime.now());
            list.add(candyKidsRecord);
        }
    }

    private void person(List<CandyKidsRecord> list) {
        CandyKidsRecord candyKidsRecord = new CandyKidsRecord();
        candyKidsRecord.setBoxId(candyBox.getId());
        candyKidsRecord.setCandyNum(candyBox.getCandyNum());
        candyKidsRecord.setCandyType(candyBox.getCandyType());
        candyKidsRecord.setNo(1);
        candyKidsRecord.setStatus(Constant.NO);
        candyKidsRecord.setState(Constant.STATE_VALID);
        candyKidsRecord.setCstCreated(LocalDateTime.now());
        candyKidsRecord.setCstModified(LocalDateTime.now());
        list.add(candyKidsRecord);
    }

    private void lucky(List<CandyKidsRecord> list){
        //可随机份额
        long candyNum = candyBox.getCandyNum() - candyBox.getUserNum();
        long tempNum = candyNum;
        List<Integer> randomList = Lists.newArrayList();
        for (int i = 0; i<candyBox.getUserNum(); i++) {
            CandyKidsRecord candyKidsRecord = new CandyKidsRecord();
            Random random = new Random();
            int candyRandom = random.nextInt(10) + 1;
            randomList.add(candyRandom);
        }
        int sum = randomList.stream().mapToInt(value -> value).sum();
        for (int i = 0; i< randomList.size(); i++) {
            CandyKidsRecord candyKidsRecord = new CandyKidsRecord();
            candyKidsRecord.setBoxId(candyBox.getId());
            candyKidsRecord.setCandyType(candyBox.getCandyType());
            candyKidsRecord.setStatus(Constant.NO);
            candyKidsRecord.setState(Constant.STATE_VALID);
            candyKidsRecord.setCstCreated(LocalDateTime.now());
            candyKidsRecord.setCstModified(LocalDateTime.now());
            long a = randomList.get(i) * candyNum / sum;
            if ((i + 1) == randomList.size()) {
                candyKidsRecord.setCandyNum(tempNum + 1);
            } else {
                tempNum = tempNum - a;
                if (tempNum > 0) {
                    candyKidsRecord.setCandyNum(a + 1);
                } else {
                    tempNum = 0;
                    candyKidsRecord.setCandyNum(tempNum + 1);
                }
            }
            list.add(candyKidsRecord);
        }
        //乱序
        Collections.shuffle(list);
        for (int i = 0; i< list.size(); i++) {
            list.get(i).setNo(i + 1);
        }
    }

    public ChatMessage createCandyMessage(ImGetUserInfo imUserInfo, Long messageId) {
        ChatMessage msg = new ChatMessage();
        CandyContent candyContent = new CandyContent();
        candyContent.setId(candyBoxVO.getId());
        candyContent.setTitle(candyBoxVO.getRemark());
        candyContent.setType(candyBoxVO.getBoxType());
        msg.setContent(JSON.toJSONString(candyContent));
        msg.setFromId(imUserInfo.getUid());
        msg.setToId(candyBoxVO.getToId());
        msg.setTransfer(true);
        msg.setType(ChatMessage.TYPE_SEND);
        msg.setMsgType(candyBoxVO.getToType());
        if (candyBoxVO.getToType().equals(ChatMessage.MSG_TYPE_UU)) {
            msg.setChatGroupId(0L);
        } else {
            msg.setChatGroupId(candyBoxVO.getToId());
        }
        msg.setId(messageId);
        msg.setUuid(UUID.randomUUID().toString());
        msg.setStatus(ChatMessage.STATUS_SEND);
        // 都以当前服务器时间为准
        msg.setDate((int) LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond());
        msg.setContentType(ChatMessage.CONTENT_TYPE_CANDY);
        msg.setUserName(imUserInfo.getUserName());
        msg.setUserAvator(imUserInfo.getLogo());
        this.getCandyBoxVO().setMessageId(messageId);
        return msg;
    }

    @Data
    public class CandyContent {
        /**
         * candyBoxId
         */
        private Long id;

        /**
         * candyBox的描述
         */
        private String title;

        /**
         * candyBox的类型
         */
        private Integer type;
    }
}
